<!--
  *  项目 树形选择组件
  *
-->
<template>
  <a-tree-select
    v-model:value="selectValue"
    :style="`width:${width}`"
    :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
    :tree-data="categoryTreeData"
    :fieldNames="{ label: 'name', key: 'id', value: 'id' }"
    :placeholder="placeholder"
    :showSearch="true"
    :tree-default-expand-all="false"
    tree-node-filter-prop="name"
    @change="onChange"
  />
</template>

<script setup lang="ts">
  import { ref, watch, onMounted } from 'vue';
  import { taskApi } from '/@/api/task/task-api';
  import { smartSentry } from '/@/lib/smart-sentry';

  const props = defineProps({
    value: String,
    placeholder: {
      type: String,
      default: '请选择',
    },
    width: {
      type: String,
      default: '100%',
    },
  });

  const emit = defineEmits(['update:value', 'change']);

  // -----------------  查询 目录 数据 -----------------
  const categoryTreeData = ref([]);
  async function queryCategoryTree() {
    try {
      let resp = await taskApi.getHasProject();
      categoryTreeData.value = resp.data;
    } catch (e) {
      smartSentry.captureError(e);
    }
  }

  // -----------------  选中相关监听、事件 -----------------
  const selectValue = ref(props.value);
  // 箭头value变化
  watch(
    () => props.value,
    (newValue) => {
      selectValue.value = newValue;
    }
  );

  function onChange(value) {
    emit('update:value', value);
    emit('change', value);
  }

  onMounted(queryCategoryTree);
</script>
